package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.common.util.R;
import com.example.buildtable.pojo.Saleschart;
import com.example.buildtable.service.SaleschartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "销售排行")
@RequestMapping(value = "/saleschart/")
public class SaleschartController {
    @Autowired
    private SaleschartService saleschartService;
    
    @Operation(summary = "查询所有销售排行数据")
    @GetMapping(value = "/getAllSaleschart")
    public R getAllSaleschart() {
        LambdaJoinQueryWrapper<Saleschart> wrapper = new LambdaJoinQueryWrapper<>(Saleschart.class);
        List<Saleschart> saleschartList = saleschartService.list();
        return R.ok("查询所有销售排行数据成功").data("saleschartList", saleschartList);
    }
    
    @Operation(summary = "增加销售排行")
    @PostMapping(value = "/addSaleschart")
    public R addSaleschart(Saleschart saleschart) {
        boolean flag = saleschartService.save(saleschart);
        if (flag) {
            return R.ok("增加销售排行成功");
        } else {
            return R.ok("增加销售排行失败");
        }
    }

    @Operation(summary = "删除销售排行")
    @GetMapping(value = "/deleteSaleschart/{salesChartId}")
    public R deleteSaleschart(@PathVariable("salesChartId") Integer salesChartId) {
        boolean flag = saleschartService.removeById(salesChartId);
        if (flag) {
            return R.ok("删除销售排行成功");
        } else {
            return R.ok("删除销售排行失败");
        }
    }

    @Operation(summary = "按照销售排行查看")
    @GetMapping(value = "/getSaleschartById/{salesChartId}")
    public R getSaleschartById(@PathVariable("salesChartId") Integer salesChartId) {
        Saleschart saleschart = saleschartService.getById(salesChartId);
        if (saleschart != null) {
            return R.ok("按照销售排行查看成功").data("saleschart", saleschart);
        } else {
            return R.error("按照销售排行查看失败");
        }
    }

    @Operation(summary = "修改销售排行")
    @PostMapping(value = "/updateSaleschart")
    public R updateSaleschart(Saleschart saleschart) {
        boolean flag = saleschartService.updateById(saleschart);
        if (flag) {
            return R.ok("修改销售排行成功");
        } else {
            return R.ok("修改销售排行失败");
        }
    }
}